#include "LidarSerializer.h"

LidarSerializer::~LidarSerializer()
{
	if (pointCloud2_.data)
	{
		delete[] pointCloud2_.data;
		pointCloud2_.data = NULL;
	}
}

int LidarSerializer::serialize(int messageType, char * message)
{

	return 0;
}

int LidarSerializer::deseralize(int messageType, const std::string & message)
{
	rapidjson::Document doc;
	doc.Parse(message.c_str());
	assert(doc.IsObject());


	pointCloud2_.hegiht = doc["height"].GetInt();
	pointCloud2_.width = doc["width"].GetInt();

	//fields
	rapidjson::Value& fields = doc["fields"];
	assert(fields.IsArray());
	for (int i = 0; i < fields.Size(); i++)
	{
		Field field;
		field.name = fields[i]["name"].GetString();
		field.offset = fields[i]["offset"].GetInt();
		field.datatype = fields[i]["datatype"].GetInt();
		field.count = fields[i]["count"].GetInt();
		pointCloud2_.fields.push_back(field);
	}
	
	pointCloud2_.is_bigendian = doc["is_bigendian"].GetInt();
	pointCloud2_.point_step = doc["point_step"].GetInt();
	pointCloud2_.row_step = doc["row_step"].GetInt();

	//data
	rapidjson::Value& data = doc["data"];
	pointCloud2_.data = new unsigned char[data.GetStringLength()];
	memcpy(pointCloud2_.data, data.GetString(), data.GetStringLength());

	pointCloud2_.is_dense = doc["is_dense"].GetInt();

	return 0;
}
